package com.wangyu.talents.web.security;

import com.auth0.jwt.exceptions.TokenExpiredException;
import com.wangyu.talents.core.entity.ResponseModel;
import org.apache.shiro.ShiroException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author wangyu
 * @Date 2018/9/13 16:46
 * @Version 1.0
 */
@RestControllerAdvice
public class ExceptionController {

  /**
   * 捕捉shiro异常
   */
  @ExceptionHandler(ShiroException.class)
  public ResponseModel noPermission() {
    return ResponseModel.newForbidden("您没有权限访问");
  }

  /**
   * 捕捉token异常 token过期处理
   */
  @ExceptionHandler(TokenExpiredException.class)
  public ResponseModel tokenFailed() {
    return ResponseModel.newTimeout("token已过期，请重新登录");
  }

  /**
   * 捕捉其他异常
   */
  @ExceptionHandler(Exception.class)
  public ResponseModel globalException(Throwable ex) {
    return ResponseModel.newError("访问出错，无法访问: " + ex.getMessage());
  }
}
